package com.onlinevideo.dao.impl;


import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.dao.UserDao;

import com.onlinevideo.entity.User;
import com.onlinevideo.util.PageUtils;
import com.onlinevideo.util.SQLUtils;

import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao implements UserDao {


    @Override
    public int insert(User user) {
        String sql = "INSERT INTO `T_user` (`username`,`password`,`phone`,`nickname`,`email`,`role_id`,`user_status`,`user_validity`,`logo_url`,`create_time`) VALUES (?,?,?,?,?,?,?,?,?,?)";
        return super.update(sql, user.getUsername(), user.getPassword(), user.getPhone(), user.getNickname(), user.getEmail(), user.getRoleId(), user.getUserStatus(), user.getUserValidity(), user.getLogoUrl(), user.getCreateTime());
    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_user` WHERE `id`=?";
        return super.update(sql, id);
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE `T_user` SET `username`=?,`email`=?,`password`=?,`phone`=?,`nickname`=?,`role_id`=?,`user_status`=?,`user_validity`=?,`logo_url`=?,`create_time`=? WHERE `id`=?";
        return super.update(sql, user.getUsername(), user.getEmail(), user.getPassword(), user.getPhone(), user.getNickname(), user.getRoleId(), user.getUserStatus(), user.getUserValidity(), user.getLogoUrl(), user.getCreateTime(), user.getId());
    }

    @Override
    public User queryById(Long id) {
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` WHERE `id`=?";
        return super.queryForOne(User.class, sql, id);
    }

    @Override
    public List<User> queryAll() {
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user`";
        return super.queryForList(User.class, sql);
    }

    @Override
    public User queryByUsernameAndPassword(User user) {
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` WHERE `username`=? AND `password`=?";
        return super.queryForOne(User.class, sql, user.getUsername(), user.getPassword());
    }

    @Override
    public Long queryByUsername(String username) {
        String sql = "SELECT count(1) FROM `T_user` WHERE `username`=? ";
        return (Long) super.queryForSingleValue(sql, username);
    }

    @Override
    public Long queryByMaxId() {
        String sql = "SELECT MAX(`id`) FROM `T_user`";
        return (Long) super.queryForSingleValue(sql);
    }


    @Override
    public List<User> queryDynamic(User user) {
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` WHERE 1=1 AND `id`=? AND `username`=? AND `password`=? AND `email`=? AND `phone`=? AND `nickname`=? AND `role_id`=? AND `user_status`=? AND `user_validity`=? AND `logo_url`=? AND `create_time`=?";
        Map map = SQLUtils.sqlValueMapping(sql, user);
        return super.queryForList(User.class, String.valueOf(map.get("sql")), (Object[]) map.get("data"));
    }

    @Override
    public List<User> queryLike(String username, String phone) {
        if (username == null) {
            username="%";
        }
        if (phone == null) {
            phone="%";
        }
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` WHERE `username` LIKE '%" + username + "%' AND `phone` LIKE '%" + phone +"%'";
//        sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` WHERE `username` LIKE `%?%` AND `phone` LIKE `%?%`";
        return super.queryForList(User.class,sql);
    }



    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT count(1) FROM `T_user`";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


    @Override
    public List<User> queryForPageItems(int begin, int length) {
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` LIMIT ?, ?";
        return super.queryForList(User.class, sql, begin, length);
    }



    @Override
    public Integer queryForPageTotalCount(User user) {
        String sql = "SELECT count(1) FROM `T_user` WHERE 1=1 AND `id`=? AND `username`=? AND `password`=? AND `email`=? AND `phone`=? AND `nickname`=? AND `role_id`=? AND `user_status`=? AND `user_validity`=? AND `logo_url`=? AND `create_time`=?";
        Map map = SQLUtils.sqlValueMapping(sql, user);
        Number number = (Number) super.queryForSingleValue(String.valueOf(map.get("sql")), (Object[]) map.get("data"));
        return number.intValue();
    }

    @Override
    public List<User> queryForPageItems(int begin, int length, User user) {
        String sql = "SELECT `id`,`username`,`password`,`email`,`phone`,`nickname`,`role_id` AS `roleId`,`user_status` AS `userStatus`,`user_validity` AS `userValidity`,`logo_url` AS `logoUrl`,`create_time` AS `createTime` FROM `T_user` WHERE 1=1 AND `id`=? AND `username`=? AND `password`=? AND `email`=? AND `phone`=? AND `nickname`=? AND `role_id`=? AND `user_status`=? AND `user_validity`=? AND `logo_url`=? AND `create_time`=?";
        Map map = SQLUtils.sqlValueMapping(sql, user);
        List<User> users = super.queryForList(User.class, String.valueOf(map.get("sql")), (Object[]) map.get("data"));
        return PageUtils.limit(begin, length, users);
    }

}
